package com.glsc.ngateway.platform.controller.cmdb;

import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDeviceDetail;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.service.cmdb.CmdbDeviceDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_DEVICE_DETAIL;
import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_WEB;

@RestController
@Api(value = "设备详情管理", tags = {"设备详情管理"})
public class CmdbDeviceDetailController {

    @Autowired
    private CmdbDeviceDetailService cmdbDeviceDetailService;
    @Autowired
    private UserCache userCache;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询设备详情
     * @param params
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/deviceDetail/search")
    @ApiOperation(value = "查询设备详情", notes = "查询设备详情")
    public PlatformResponse<Object> searchDeviceDetail(@RequestBody Object params) {
        Object result = cmdbDeviceDetailService.search(CMDB_DEVICE_DETAIL, params);
        return PlatformResponse.successData(result);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deviceDetail/findByDeviceDetailCode")
    @ApiOperation(value = "通过规格号查询设备标准", notes = "通过规格号查询设备标准")
    public PlatformResponse<Object> findByDeviceDetailCode(@RequestParam String deviceDetailCode) {
        Object result = cmdbDeviceDetailService.findByDeviceDetailCode(deviceDetailCode);
        return PlatformResponse.successData(result);
    }

    @RequestMapping(value = "/deviceDetail/queryAll")
    @ApiOperation(value = "查询设备详情", notes = "查询设备详情")
    public PlatformResponse<Object> queryAll() {
        List<CmdbDeviceDetail> result = cmdbDeviceDetailService.queryAll();
        return PlatformResponse.successData(result);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deviceDetail/addOrUpdate")
    @ApiOperation(value = "修改设备详情", notes = "修改设备详情")
    public PlatformResponse<Void> addOrUpdateDeviceDetail(@RequestBody CmdbDeviceDetail cmdbDeviceDetail) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDeviceDetailService.addOrUpdate(cmdbDeviceDetail, account, CMDB_WEB);
        cmdbDeviceDetailService.deviceDetailWithPartIn(cmdbDeviceDetail,account,CMDB_WEB);
        return PlatformResponse.successMsg("处理成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deviceDetail/delete")
    @ApiOperation(value = "删除设备详情", notes = "删除设备详情")
    public PlatformResponse<Void> deleteDeviceDetail(@RequestBody CmdbDeviceDetail cmdbDeviceDetail) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDeviceDetailService.delete(cmdbDeviceDetail, account);
        return PlatformResponse.successMsg("删除成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deviceDetail/deleteList")
    @ApiOperation(value = "批量删除设备详情", notes = "批量删除设备详情")
    public PlatformResponse<Void> deleteListDeviceDetail(@RequestBody List<String> codes) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDeviceDetailService.deleteByCodeList(codes, account);
        return PlatformResponse.successMsg("删除成功");
    }
}
